﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


/// <summary>
/// Summary description for PasswordExpiry
/// </summary>
public class PasswordExpiry
{
	public PasswordExpiry()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    

    public static bool Expireday (string username)
    {
        bool result = false;
            


        try
        {
            
           
            string strConnection = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
            SqlConnection sqlConnection = new SqlConnection(strConnection);
        
           String balancedaysqlquery = "SELECT DATEDIFF(day,(SELECT TOP(1) changeDate FROM PasswordHistory WHERE userName = @UserName ORDER BY changeDate DESC),GETDATE()) AS DiffDate";
           String gspasswordexpiry = "Select PasswordExpiry from [GlobalSetting]";

           SqlCommand command = new SqlCommand(balancedaysqlquery, sqlConnection);
           SqlCommand command1 = new SqlCommand(gspasswordexpiry, sqlConnection);

           SqlParameter findusernameparameter = new SqlParameter("@UserName", SqlDbType.VarChar);
           findusernameparameter.Value = username;
           command.Parameters.Add(findusernameparameter);
           sqlConnection.Open();
          string balanceday = command.ExecuteScalar().ToString();
          
          string settingsday = command1.ExecuteScalar().ToString();
          sqlConnection.Close();
          
            int x = Convert.ToInt32(balanceday);
            int y = Convert.ToInt32(settingsday);
            
            if (x >= y)
            {
                result = true; // Password expire
                
               
               return result;
            
            
            }
            else
            {
                result = false; //Pasword not expire


            }


           


        }



        catch (Exception ex)
        {
            string errorMessage;
            errorMessage = ("The following error occured :rn" + ex.ToString());
            throw new Exception(errorMessage);
        }
        return result;
    }
    
}